package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.SmallPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.MD5Utils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void add(User user,Integer[] roleIds) {
        String password = user.getPassword();
        if (password != null) {
            //加密
            //使用md5将明文密码进行加密
            password = MD5Utils.md5(password);
            user.setPassword(password);
        }
        try {
            userDao.add(user);
        } catch (Exception e) {
            e.printStackTrace();
    }

        Integer userId = user.getId();
        this.setUserIdsAndRoleIds(userId,roleIds);
    }

    @Override
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);
        return user;
    }

    @Override
    public List<User> findAll() {
        return null;
    }




    @Override
    public PageResult findAll(SmallPageBean smallPageBean) {
        //当前页
        Integer smallCurrentPage = smallPageBean.getSmallCurrentPage();
        //每页显示的条数
        Integer smallPageSize = smallPageBean.getSmallPageSize();

        //分页插件助手
        PageHelper.startPage(smallCurrentPage,smallPageSize);
        Page<User> users = userDao.findAll();

        //总条数
        long total = users.getTotal();
        //所有的ID
        List result = users.getResult();
        return new PageResult(total,result);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        //每页显示几条数据
        Integer pageSize = queryPageBean.getPageSize();
        //查询条件
        String queryString = queryPageBean.getQueryString();

        //分页助手插件
        PageHelper.startPage(currentPage,pageSize);

        Page<User> list = userDao.findByCondition(queryString);
        //总条数
        long total = list.getTotal();
        //全部数据
        List<User> result = list.getResult();

        return new PageResult(total,result);
    }

    public void setUserIdsAndRoleIds(Integer userId,Integer[] roleIds) {
        if (roleIds != null && roleIds.length > 0 ) {
            for (Integer roleId : roleIds) {
                //封装到map集合，sql语句中的参数就是map类型，键值对
                Map<String, Integer> map = new HashMap<>();
                map.put("userId", userId);
                map.put("roleId", roleId);
                userDao.setUserIdsAndRoleIds(map);
            }
        }
    }
}
